package com.cndw.kungfu.model.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Component;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.dao.LogDao;
import com.cndw.kungfu.domain.Event;
import com.cndw.kungfu.domain.LogCost;
import com.cndw.kungfu.domain.LogGot;
import com.cndw.kungfu.domain.LogPlayer;
import com.cndw.kungfu.ext.LogUtil;

@Component
public class LogManager {
	@Autowired
	private LogDao dao;

	public void saveLogPlayer(List<LogPlayer> logPlayers) {
		try {
			dao.saveLogPlayer(logPlayers);
		} catch (Exception e) {
			for (LogPlayer elog : logPlayers) {
				if (elog.getParams().length() >= 400) {
					LogUtil.getSysErr().error("log err " + elog.getPlayerId() + " | " + elog.getEventId() + " | " + elog.getParams());
				}
			}
			ConstantsSystem.throwExceptionDB(e);
		}

	}

	public boolean saveLogCost(LogCost logCost) {

		try {
			dao.saveLogCost(logCost);
		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

	public List<Integer> getBuyIds(long playerId) {
		try {
			return dao.selectBuyIds(playerId);
		} catch (Exception e) {
		}
		return null;
	}

	public List<LogPlayer> getLogs(long playerId, int objectId, int eventId, String param, int beginTime, int endTime, int offset, int limit,String ip) {
		try {
			if (!BlankUtils.isBlank(param)) {
				param = "%" + param + "%";
			}
			return dao.getLogs(playerId, objectId, eventId, param, beginTime, endTime, offset, limit,ip);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<LogPlayer>(1);
		}
	}

	public int getLogCounts(long playerId, int objectId, int eventId, String param, int beginTime, int endTime,String ip) {
		try {
			if (!BlankUtils.isBlank(param)) {
				param = "%" + param + "%";
			}
			return dao.getLogCounts(playerId, objectId, eventId, param, beginTime, endTime,ip);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public int selectLastBuyTime(long playerId, int goodsId) {
		try {
			return dao.selectLastBuyTime(playerId, goodsId);
		} catch (IncorrectResultSizeDataAccessException e) {

		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public boolean saveLogGot(LogGot logGot) {
		try {
			dao.saveLogGot(logGot);
		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

	public int selectCostSum(long playerId, int beginTime, int endTime, int[] ids) {
		try {
			return dao.selectCostSum(playerId, beginTime, endTime, ids);
		} catch (NullPointerException e) {

		} catch (Exception e) {
			e.printStackTrace();

		}
		return 0;
	}

	public void clearLog30() {
		try {
			int limitSize = ConstantsLog.clearSize;
			
			dao.clearLog0(ConstantsLog.clearIds,limitSize);
			dao.clearLog1(ConstantsLog.clearIds,limitSize);
			dao.clearLog2(ConstantsLog.clearIds,limitSize);
			dao.clearLog3(ConstantsLog.clearIds,limitSize);
			dao.clearLog4(ConstantsLog.clearIds,limitSize);
			dao.clearLog5(ConstantsLog.clearIds,limitSize);
			dao.clearLog6(ConstantsLog.clearIds,limitSize);
			dao.clearLog7(ConstantsLog.clearIds,limitSize);
			dao.clearLog8(ConstantsLog.clearIds,limitSize);
			dao.clearLog9(ConstantsLog.clearIds,limitSize);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	public List<LogPlayer> getLogsAll(int eventId,int beginTime,int endTime){
		List<LogPlayer> allLogs = new ArrayList<LogPlayer>();
		try {
			List<LogPlayer> tmpList;
			
			try {
				tmpList = dao.getAllByType0(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType1(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType2(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType3(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType4(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType5(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType6(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType7(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType8(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				tmpList = dao.getAllByType9(eventId, beginTime, endTime);
				if (tmpList != null) {
					allLogs.addAll(tmpList);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return allLogs;
	}
	
	
	public Event getEvent(long uid,String title,int addTime){
		try {
			return dao.getEvent(uid, title, addTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void delEvent(long uid,int id){
		try {
			dao.delEvent(uid, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
